<?php
	/*
	Plugin Name: ToboAds widget
	Description: Adds a sidebar widget with ToboAds (www.toboads.com) by Tobonet (www.tobonet.com)
	Version: 1.2
	Author: Domen Kosir domen.kosir@tobonet.com
	Author URI: http://domn.sopca.com
	*/

	/**
	This file is part of ToboAds plugins.
	
	ToboAds plugins is free software; you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation; either version 2.1 of the License, or
	(at your option) any later version.
	
	ToboAds plugins is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with WideImage; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
	**/



// ----------------
// global variables
$toboads_domain = 'toboads';
if (!isset($toboads_counter))
	$toboads_counter = 0;
if (!isset($toboads_counter_max))
	$toboads_counter_max = 3;
if (!isset($toboads_allowed_ad_sizes))
	$toboads_allowed_ad_sizes = array('180x150','234x60','300x250','336x280','468x60','728x90','120x600','120x240','160x240','125x125','160x600','250x250');



// ----------------
// validates the toboads publisher id
if (!function_exists('toboads_is_valid_publisher_id'))
{
	function toboads_is_valid_publisher_id($publisher_id)
	{
		if (ereg("^[a-fA-F0-9]{32}$", $publisher_id) !== false)
			return true;
		return false;
	}
}



// ----------------
// validates colors
if (!function_exists('toboads_is_valid_color'))
{
	function toboads_is_valid_color($color)
	{
		if (ereg("^#?[a-fA-F0-9]{3}$", $color) !== false) // RGB code
			return true;
		if (ereg("^#?[a-fA-F0-9]{6}$", $color) !== false) // RGB code
			return true;
		return false;
	}
}



// ----------------
// returns the suffix (backwards compatibility)
if (!function_exists('toboads_get_suffix'))
{
	function toboads_get_suffix($number)
	{
		if ($number == 1)
			return "";
		return "$number";
	}
}



// ----------------
function widget_toboads_init()
{
	if (!function_exists('register_sidebar_widget'))
		return;
	
	
	
	// ----------------
	// this is the function that outputs the ads.
	function widget_toboads($args, $number = 1)
	{
		global $toboads_counter, $toboads_counter_max, $toboads_allowed_ad_sizes;
		$suffix = toboads_get_suffix($number);
		
		extract($args);
		
		$options = get_option('widget_toboads');
		
		// publisher id
		$publisher_id = $options['publisher_id'.$suffix];
		// size
		$ad_size = $options['ad_size'.$suffix];
		if (!in_array($ad_size, $toboads_allowed_ad_sizes)) $ad_size = '120x240';
		// type
		$options['type_text'.$suffix] = ($options['type_text'.$suffix] !== '0' ? 1 : 0);
		$type_text = $options['type_text'.$suffix];
		$options['type_shop'.$suffix] = ($options['type_shop'.$suffix] !== '0' ? 1 : 0);
		$type_shop = $options['type_shop'.$suffix];
		$options['type_rich'.$suffix] = ($options['type_rich'.$suffix] !== '0' ? 1 : 0);
		$type_rich = $options['type_rich'.$suffix];
		// colors
		$color_border = $options['color_border'.$suffix];
		$color_bg = $options['color_bg'.$suffix];
		$color_title = $options['color_title'.$suffix];
		$color_text = $options['color_text'.$suffix];
		$color_link = $options['color_link'.$suffix];
		
		// generate javascript string
		
		$string = "<div align=\"center\">\n";
		$string .= "<!-- ToboAds -->\n";
		$string .= "<script language=\"javascript\" type=\"text/javascript\">\n";
		$string .= "<!--\n";
		// publisher id
		$string .= "	toboads_publisher_id = '$publisher_id';\n";
		// size
		$string .= "	toboads_ad_format = '$ad_size';\n";
		// type
		if ($type_text == '') $type_text = 1;
		else $type_text = intval($type_text);
		$string .= "	toboads_ad_type_text = '".($type_text == 1 ? "true" : "false")."';\n";
		if ($type_shop == '') $type_shop = 1;
		else $type_shop = intval($type_shop);
		$string .= "	toboads_ad_type_shop = '".($type_shop == 1 ? "true" : "false")."';\n";
		if ($type_rich == '') $type_rich = 1;
		else $type_rich = intval($type_rich);
		$string .= "	toboads_ad_type_rich = '".($type_rich == 1 ? "true" : "false")."';\n";
		// colors
		if (strlen($color_border) > 0)
			$string .= "	toboads_color_border = '$color_border';\n";
		if (strlen($color_bg) > 0)
			$string .= "	toboads_color_bg = '$color_bg';\n";
		if (strlen($color_title) > 0)
			$string .= "	toboads_color_title = '$color_title';\n";
		if (strlen($color_link) > 0)
			$string .= "	toboads_color_link = '$color_link';\n";
		if (strlen($color_text) > 0)
			$string .= "	toboads_color_text = '$color_text';\n";
		$string .= "-->\n";
		$string .= "</script>\n";
		$string .= "<script language=\"javascript\" type=\"text/javascript\" src=\"http://relay-si.toboads.com/\"></script>\n";
		$string .= "<!-- /ToboAds -->\n";
		$string .= "</div>\n";
		
		
		if ($toboads_counter < $toboads_counter_max && toboads_is_valid_publisher_id($publisher_id))
		{
			echo $before_widget;
			echo $string; 
			echo $after_widget;
			$toboads_counter++;
		}
	}
	
	
	
	// ----------------
	// this function outputs the form to let the users edit toboads options
	function widget_toboads_control($number)
	{
		global $toboads_counter, $toboads_counter_max, $toboads_allowed_ad_sizes, $toboads_domain;
		$suffix = toboads_get_suffix($number);
		
		$options = get_option('widget_toboads');
		
		// Process POST data
		if ($_POST['toboads-submit'.$suffix])
		{
			// publisher id
			if (toboads_is_valid_publisher_id($_POST['toboads-publisher_id'.$suffix])) $options['publisher_id'.$suffix] = $_POST['toboads-publisher_id'.$suffix];
			// size
			$options['ad_size'.$suffix] = $_POST['toboads-ad_size'.$suffix];
			if (!in_array($options['ad_size'.$suffix], $toboads_allowed_ad_sizes)) $options['ad_size'.$suffix] = '120x240';
			// type
			$options['type_text'.$suffix] = $_POST['toboads-type_text'.$suffix] == 'on' ? '1' : '0';
			$options['type_shop'.$suffix] = $_POST['toboads-type_shop'.$suffix] == 'on' ? '1' : '0';
			$options['type_rich'.$suffix] = $_POST['toboads-type_rich'.$suffix] == 'on' ? '1' : '0';
			// colors
			if (toboads_is_valid_color($_POST['toboads-color_border'.$suffix])) $options['color_border'.$suffix] = trim($_POST['toboads-color_border'.$suffix], '#');
			else $options['color_border'.$suffix] = '';
			if (toboads_is_valid_color($_POST['toboads-color_bg'.$suffix])) $options['color_bg'.$suffix] = trim($_POST['toboads-color_bg'.$suffix], '#');
			else $options['color_bg'.$suffix] = '';
			if (toboads_is_valid_color($_POST['toboads-color_title'.$suffix])) $options['color_title'.$suffix] = trim($_POST['toboads-color_title'.$suffix], '#');
			else $options['color_title'.$suffix] = '';
			if (toboads_is_valid_color($_POST['toboads-color_text'.$suffix])) $options['color_text'.$suffix] = trim($_POST['toboads-color_text'.$suffix], '#');
			else $options['color_text'.$suffix] = '';
			if (toboads_is_valid_color($_POST['toboads-color_link'.$suffix])) $options['color_link'.$suffix] = trim($_POST['toboads-color_link'.$suffix], '#');
			else $options['color_link'.$suffix] = '';
			
			update_option('widget_toboads', $options);
		}
		
		// publisher id
		$publisher_id = $options['publisher_id'.$suffix];
		if (strlen($publisher_id) == 0) $publisher_id = $options['publisher_id'];
		// size
		$ad_size = $options['ad_size'.$suffix];
		// type
		$options['type_text'.$suffix] = ($options['type_text'.$suffix] !== '0' ? 1 : 0);
		$type_text = $options['type_text'.$suffix];
		$options['type_shop'.$suffix] = ($options['type_shop'.$suffix] !== '0' ? 1 : 0);
		$type_shop = $options['type_shop'.$suffix];
		$options['type_rich'.$suffix] = ($options['type_rich'.$suffix] !== '0' ? 1 : 0);
		$type_rich = $options['type_rich'.$suffix];
		// colors
		$color_border = $options['color_border'.$suffix];
		$color_bg = $options['color_bg'.$suffix];
		$color_title = $options['color_title'.$suffix];
		$color_text = $options['color_text'.$suffix];
		$color_link = $options['color_link'.$suffix];
		
		// Display the control form
		printf(__('You can have up to %d ad blocks per page.<br />Each of the following attributes can be changed for every ad block.', $toboads_domain), $toboads_counter_max);
		echo '<br />';
		echo '<br />';
		//echo 'You can have up to '.$toboads_counter_max.' ad blocks per page.<br />Each of the following attributes can be changed for every ad block.';
		echo '<table border="0" cellspacing="4" cellpadding="1">';
		// publisher_id
		echo '<tr>';
		echo '<td align="right"><label for="toboads-publisher_id'.$suffix.'">'.__('Publisher ID', $toboads_domain).': </label></td>';
		echo '<td align="left"><input style="width: 270px;" maxlength="32" id="toboads-publisher_id'.$suffix.'" name="toboads-publisher_id'.$suffix.'" type="text" value="'.$publisher_id.'" />'.(toboads_is_valid_publisher_id($publisher_id) ? '' : '<br /><span style="color: red;">Invalid publisher ID</span>').'</td>';
		echo '</tr>';
		// size
		echo '<tr>';
		echo '<td align="right"><label for="toboads-ad_size'.$suffix.'">'.__('Ad size', $toboads_domain).': </label></td>';
		echo '<td align="left"><select id="toboads-ad_size'.$suffix.'" name="toboads-ad_size'.$suffix.'">'; foreach($toboads_allowed_ad_sizes as $tmp_ad_size) { if ($ad_size == $tmp_ad_size) $sel = 'selected="selected"'; else $sel = ''; echo '<option value="'.$tmp_ad_size.'" '.$sel.'>'.$tmp_ad_size.'</option>'; } echo '</select></td>';
		echo '</tr>';
		// type
		echo '<tr>';
		echo '<td align="right">'.__('Ad type', $toboads_domain).': </label></td>';
		echo '<td align="left">';
		if ($type_text == 1) $type_text_chk = 'checked="checked"';
		else $type_text_chk = '';
		echo '<input type="checkbox" id="toboads-type_text'.$suffix.'" name="toboads-type_text'.$suffix.'" value="on" '.$type_text_chk.' /> <label for="toboads-type_text'.$suffix.'">'.__('text ads', $toboads_domain).'</label>';
		echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		if ($type_shop == 1) $type_shop_chk = 'checked="checked"';
		else $type_shop_chk = '';
		echo '<input type="checkbox" id="toboads-type_shop'.$suffix.'" name="toboads-type_shop'.$suffix.'" value="on" '.$type_shop_chk.' /> <label for="toboads-type_shop'.$suffix.'">'.__('shop ads', $toboads_domain).'</label>';
		echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		if ($type_rich == 1) $type_rich_chk = 'checked="checked"';
		else $type_rich_chk = '';
		echo '<input type="checkbox" id="toboads-type_rich'.$suffix.'" name="toboads-type_rich'.$suffix.'" value="on" '.$type_rich_chk.' /> <label for="toboads-type_rich'.$suffix.'">'.__('rich ads', $toboads_domain).'</label>';
		echo '</td>';
		echo '</tr>';
		// colors
		echo '<tr>';
		echo '<td align="right">'.__('Border color', $toboads_domain).': </td>';
		echo '<td align="left"><input style="width: 70px;" id="toboads-color_border'.$suffix.'" name="toboads-color_border'.$suffix.'" type="text" value="'.$color_border.'" /></td>';
		echo '</tr>';
		echo '<tr>';
		echo '<td align="right">'.__('Background color', $toboads_domain).': </td>';
		echo '<td align="left"><input style="width: 70px;" id="toboads-color_bg'.$suffix.'" name="toboads-color_bg'.$suffix.'" type="text" value="'.$color_bg.'" /></td>';
		echo '</tr>';
		echo '<tr>';
		echo '<td align="right">'.__('Title color', $toboads_domain).': </td>';
		echo '<td align="left"><input style="width: 70px;" id="toboads-color_title'.$suffix.'" name="toboads-color_title'.$suffix.'" type="text" value="'.$color_title.'" /></td>';
		echo '</tr>';
		echo '<tr>';
		echo '<td align="right">'.__('Text color', $toboads_domain).': </td>';
		echo '<td align="left"><input style="width: 70px;" id="toboads-color_text'.$suffix.'" name="toboads-color_text'.$suffix.'" type="text" value="'.$color_text.'" /></td>';
		echo '</tr>';
		echo '<tr>';
		echo '<td align="right">'.__('Link color', $toboads_domain).': </td>';
		echo '<td align="left"><input style="width: 70px;" id="toboads-color_link'.$suffix.'" name="toboads-color_link'.$suffix.'" type="text" value="'.$color_link.'" /></td>';
		echo '</tr>';
		
		echo '</table>';
		echo '<input type="hidden" id="toboads-submit'.$suffix.'" name="toboads-submit'.$suffix.'" value="1" />';
	}
	
	
	
	// ----------------
	// this function updates the number of ToboAds widgets
	function widget_toboads_setup()
	{
		global $toboads_counter_max;
		
		$options = $newoptions = get_option('widget_toboads');
		if (isset($_POST['toboads-number-submit']))
		{
			$number = intval($_POST['toboads-number']);
			if ( $number > $toboads_counter_max ) $number = $toboads_counter_max;
			if ( $number < 1 ) $number = 1;
			$newoptions['number'] = $number;
		}
		if ($options != $newoptions)
		{
			$options = $newoptions;
			update_option('widget_toboads', $options);
			widget_toboads_register($options['number']);
		}
	}
	
	
	
	// ----------------
	// this function lets the user to decide how many ToboAds widgets he wants
	function widget_toboads_page()
	{
		global $toboads_counter_max, $toboads_domain;
		
		$options = $newoptions = get_option('widget_toboads');
?>
	<div class="wrap">
		<form method="POST">
			<h2><?php _e('Toboads Widgets', $toboads_domain); ?></h2>
			<p style="line-height: 30px;">
				<?php _e('How many ToboAds widgets would you like?', $toboads_domain); ?>
				<select id="toboads-number" name="toboads-number" value="<?php echo $options['number']; ?>">
<?php for ( $i = 1; $i <= $toboads_counter_max; $i++ ) echo "<option value='$i' ".($options['number']==$i ? 'selected="selected"' : '').">$i</option>"; ?>
				</select>
				<span class="submit"><input type="submit" name="toboads-number-submit" id="toboads-number-submit" value="<?php _e('Save', $toboads_domain); ?>" /></span>
			</p>
		</form>
	</div>
<?php
	}
	
	
	
	// ----------------
	// this function registers the ToboAds widget
	function widget_toboads_register()
	{
		global $toboads_counter_max;
		
		$options = get_option('widget_toboads');
		$number = intval($options['number']);
		if ($number < 1) $number = 1;
		if ($number > $toboads_counter_max) $number = $toboads_counter_max;
		for ($i = 1; $i <= $number; $i++)
		{
			$name = array('Toboads'.toboads_get_suffix($i), null, $i);
			register_sidebar_widget($name, $i <= $toboads_counter_max ? 'widget_toboads' : /* unregister */ '', $i);
			register_widget_control($name, $i <= $toboads_counter_max ? 'widget_toboads_control' : /* unregister */ '', 500, 400, $i);
		}
		
		add_action('sidebar_admin_setup', 'widget_toboads_setup');
		add_action('sidebar_admin_page', 'widget_toboads_page');
	}
	
	
	load_plugin_textdomain('toboads', 'wp-content/plugins/widgets');
	widget_toboads_register();
}

// run the code later in case this loads prior to any required plugins
add_action('plugins_loaded', 'widget_toboads_init');
?>